package com.ruoyi.controller.archives;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.archives.PAdoptRecord;
import com.ruoyi.service.archives.PAdoptRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName PAdoptRecord
 * @Description 领养记录类
 * @Date 2022/06/20 16:12
 */
@RestController
@RequestMapping("/archives/pAdoptRecord")
public class PAdoptRecordController extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PAdoptRecordService adoptRecordService;

    /**
     * 分页模糊查询所领养记录
     * @param pAdoptRecord 领养信息
     * @return 所有领养的记录
     */
//    @PreAuthorize("@ss.hasPermi('')")
    @GetMapping
    public TableDataInfo getPageInfo( PAdoptRecord pAdoptRecord) {
        startPage();
        List<PAdoptRecord> list = adoptRecordService.getPageInfo(pAdoptRecord);
        return getDataTable(list);
    }


    /**
     *  根据手机号以及猪耳标号添加领养信息
     * @param  record 手机号
     * @param record 猪耳标
     * @return 受影响的行数
     */
//    @PreAuthorize("@ss.hasPermi('')")
    @PostMapping("/createInfo")
    public AjaxResult appendRecord(@RequestBody(required=false) PAdoptRecord record) {

        logger.info("领养人手机号以及猪耳标号 ======>{}======>{}", record.getAdoptPhone(), record.getEarTag());

        //  判断猪是否已经被领养
        int result = adoptRecordService.hasAdopted(record.getEarTag());
        if (result > 0) {
            return AjaxResult.error("该猪已被领养");
        }

        int row = adoptRecordService.appendRecord(record.getAdoptPhone(), record.getEarTag());
        if (row <= 0) {
            logger.error("添加领养记录失败");
            return AjaxResult.error("添加领养记录失败");
        }
        return AjaxResult.success("添加领养记录成功");
    }

    /**
     * 根据id修改领养记录
     * @param adoptRecord 领养信息
     * @return 受影响的行数
     */
//    @PreAuthorize("@ss.hasPermi('')")
    @PutMapping
    public AjaxResult updateRecordInfoById(@RequestBody PAdoptRecord adoptRecord) {
        logger.info("更新时传参时领养记录信息 =======> {}", adoptRecord);
        //  判断猪是否已经被领养
        int result = adoptRecordService.hasAdopted(adoptRecord.getEarTag());
        if (result > 0) {

            return AjaxResult.error("该猪已被领养");
        }

        if (adoptRecord.isDelFlag()) {
            logger.error("记录可能已经被删除  =====> {}", adoptRecord);
            return AjaxResult.error("该记录不存在");
        }

        int row = adoptRecordService.updateRecordInfoById(adoptRecord);
        if (row <= 0) {
            return AjaxResult.error("修改记录失败");
        }
        return AjaxResult.success("修改记录成功");
    }


    /**
     *  根据id(或多个)删除领养记录
     * @param recordId 要删除的id
     * @return 受影响的行数
     */
//    @PreAuthorize("@ss.hasPermi('')")
    @DeleteMapping("/{recordId}")
    public AjaxResult deleteRecordById( @PathVariable Long[] recordId) {
        logger.info("要删除的记录id =======> {}", recordId);
        int row = adoptRecordService.deleteRecordById(recordId);
        
        if (row <= 0) {
            return AjaxResult.error("删除记录失败");
        }
        
        return AjaxResult.success("删除记录成功");
    }
}
